package com.jilujia.web.controller;

import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jilujia.domain.hotel.Hotel;
import com.jilujia.domain.hotel.service.HotelService;
import com.jilujia.domain.user.User;
import com.jilujia.domain.user.service.UserService;

@Controller
public class IndexController extends AbstractWebController {

	@Autowired
	private HotelService hotelService;

	@Autowired
	private UserService userService;

	private static final Logger LOG = LoggerFactory.getLogger(IndexController.class);

	public IndexController() {
	}

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String index(@CookieValue(required = false) String location, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		LOG.debug("index in ip:{}", request.getHeader("X-Real-IP"));
		LOG.debug("index in user agent:{}", request.getHeader("user-agent"));

		if (location == null) {
			LOG.debug("1");
			location = "SHA";
			response.addCookie(new Cookie("location", location));
		}

		List<Hotel> hotelList = hotelService.findTop10Hotels();
		List<User> userList = userService.findTop10Users();

		model.addAttribute(hotelList);
		model.addAttribute(userList);

		return "index";
	}

}
